/* Copyright (c) 2006 YourNameHere
   See the file LICENSE.txt for licensing information. */

Components.utils.import("resource://nhnjapanpartnertoolbar/common.js")
Components.utils.import("resource://nhnjapanpartnertoolbar/log4moz.js")

var JpToolbarCallback = {
    
    sentOpeCode2Toolbar:function(opId, opCode){
        
    }
}

function JsStudy(){
    
    this._logger = Log4Moz.repository.getLogger("XULSchool.SomeObject");  
    this._logger.level = Log4Moz.Level["All"];  
    this._prefService =  
        Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
    this._prefService.QueryInterface(Ci.nsIPrefBranch2);
    this._prefService.addObserver("extensions.nhnjapanpartnertoolbar.message.count", this, false);
    this._prefService.QueryInterface(Ci.nsIPrefBranch);
    this.xpcomservice = null;
}

JsStudy.prototype.InitEnv = function(){
    
    try {
            JpToolbarCallback.targetWindow=window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
             .getInterface(Components.interfaces.nsIWebNavigation)
             .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
             .treeOwner
             .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
             .getInterface(Components.interfaces.nsIXULWindow)
             .docShell
             .QueryInterface(Components.interfaces.nsIBaseWindow);
             
            //JpToolbarCallback.targetWindow=window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
            // .getInterface(Components.interfaces.nsIWebNavigation)
            // .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
            // .treeOwner
            // .QueryInterface(Components.interfaces.nsIBaseWindow);
             
             JpToolbarCallback.userAgent=window.navigator.userAgent;
             
            var g_service = null;
            const ENIG_CONTRACTID = "@nhn.com/MyComponent;1";
            
            g_service = Components.classes[ENIG_CONTRACTID].createInstance(Components.interfaces.IMyComponent);
            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            this.xpcomservice = g_service.QueryInterface(Components.interfaces.IMyComponent);
            this.xpcomservice.registerToolbarCallback(JpToolbarCallback);
            
	} catch (err) {
	    alert(err);
	}
}

JsStudy.prototype.errorHandleFunction = function(msg, url, line){
    
    alert(msg + "\n" + url + "\n" + line);
}

JsStudy.prototype.showMyAlert = function(){
    
    var x = -1;
    x = NHNJapanPartnerToolbarChrome.BrowserOverylay.otherWinowModify;
    alert("my Alert" + x);
    
    NHNJapanPartnerToolbarChrome.BrowserOverylay.otherWinowModify = 12;
    
    alert("my Alert" + NHNJapanPartnerToolbarChrome.BrowserOverylay.otherWinowModify);
}

JsStudy.prototype.showUrl = function(){
    window.alert("fds");
    // go to usr in current window
    //window.content.document.location.href = "http://www.soufun.com";
    
    //get current window's url
    //alert(window.content.document.location.href);
    
    // window's size
    //alert(""+window.screenX + "," + window.screenY + ","+ window.outerWidth+","+window.outerHeight);
    
    // screen
    //alert("" + window.screen.availWidth + "," + window.screen.availHeight);
    
    // navigator
        //var b = "Browser Infor\n";
        //for(var propname in window.navigator){
        //    if(window.navigator[propname].toString){
        //        b += propname + ":" + window.navigator[propname].toString() + "\n";
        //    }
        //}
        
    
    // open new tab
    //document.getElementById("content").addTab("http://www.tianya.cn");
    gBrowser.selectedTab = gBrowser.addTab("http://www.tianya.cn");
    
    
    
    // status failure
    //try{
    //    window.status ="fdasfdsa";
    //  window.defaultStatus ="fdasfdsa";
    //}
    //catch(err){
    //    alert(err);
    //}
    
    
    
    
    
    // modify window's size
    //window.outerWidth = 500;
    //window.outerHeight = 400;
    
    
    //window.history.go(-1);
    //window.history.back();
    //browser.goBack();
    //content.doucment.history.goBack();
    
    //location.href
}

JsStudy.prototype.showNotificationBox = function(aEvent){
    let nb = gBrowser.getNotificationBox();
    let acceptBtn = new Object();
    let declineBtn = new Object();
    
    let that = this;
    
    acceptBtn.label = "Accept";
    acceptBtn.popup = null;
    acceptBtn.callback = function(){};
    
    declineBtn.label = "Decline";
    declineBtn.callback=function(){};
    
    nb.appendNotification("notification message", "Japanpartnertoolbarnotification",
                          null,
                          nb.PRIORITY_INFO_LOW,
                          [acceptBtn,declineBtn] );
}

JsStudy.prototype.showAlert = function(aEvent){
    window.alert("fa");
    return;

    let s =  Components.classes["@mozilla.org/alerts-service;1"].getService(Components.interfaces.nsIAlertsService);
    
    s.showAlertNotification("chrome://nhnjapanpartnertoolbar/skin/btn_option.ico","abc","abcd",false," ", null, "fdsaddd");
    //s.showAlertNotification()
    
    //s.showAlertNotification("","title", "message", false, "", null);
    
}

JsStudy.prototype.parseJson = function(aEvent){
        var myText = '{"shops": [{"name": "Apple", "code": "A001"}, {"name": "Orange"}], "total": 100}';
    let jsObject = JSON.parse(myText);
    window.alert(jsObject.shops[0].name);
    return;
}

JsStudy.prototype.createFolder = function(aEvent){
    //alert("create folder");
    let dirService = Cc[DirIO.dirservCID].getService(DirIO.propsIID);
    let profileDir = dirService.get("ProfD", DirIO.fileIID);
    let prompts =
        Cc["@mozilla.org/embedcomp/prompt-service;1"].getService(Ci.nsIPromptService);
        
    var retValue = {value:""};
    var check = {value: false};
    var result = prompts.prompt(window,
                                "Create Directory",
                                "Please input directory name",
                                retValue,
                                null,
                                check);
    if (result){
        profileDir.append(retValue.value);
        var d = DirIO.open(profileDir.path);
        if(d.exists()){
            alert(d + " already exist")
        }
        else{
            var rv = DirIO.create(d);
            if(rv){
                alert("succeed to create folder: " + d.path);
            }
            else{
                alert("fail to create folder: " + d.path);
            }
        }  
    }
    else{
        
    }
}

JsStudy.prototype.createFile = function(aEvent){
    //alert("create file");
    this._logger.fatal("This is a fatal message.");  
    this._logger.error("This is an error message.");  
    this._logger.warn("This is a warning message.");  
    this._logger.info("This is an info message.");  
    this._logger.config("This is a config message.");  
    this._logger.debug("This is a debug message.");  
    this._logger.trace("This is a trace message.");

    alert("succeed");
}

JsStudy.prototype.readWritePref = function(aEvent){
    //try{
    //        let application =
    //    Cc["@mozilla.org/fuel/application;1"].getService(Ci.fuelIApplication);
    //
    //var num = application.prefs.get("extensions.nhnjapanpartnertoolbar.message.count");
    //alert(num);
    //}
    //catch(err){
    //    alert(err);
    //}
    
    //alert(" " + aEvent.clientX + "," + aEvent.clientY);
    //alert(aEvent.clientX.toString());
    //aEvent.target.setAttribute("label", "fdasfdsa");
    
    
    
    var num = this._prefService.getIntPref("extensions.nhnjapanpartnertoolbar.message.count");
    num += 1;
    this._prefService.setIntPref("extensions.nhnjapanpartnertoolbar.message.count", num);
}

JsStudy.prototype.observe = function(subject, aTopic, aData){
    alert("change");
}

JsStudy.prototype.layoutDialog = function(aEvent){
    
    var feature = "centerscreen, chrome, dialog=no,modal";
    var dlg = window.openDialog("chrome://nhnjapanpartnertoolbar/content/layoutdialog.xul",
                 "dlg",
                 feature,
                 nhnJsStudy);
}


JsStudy.prototype.layoutDialog2 = function(aEvent){
    
    var feature = "centerscreen, chrome, dialog=no,modal,resizable=yes";
    var dlg = window.openDialog("chrome://nhnjapanpartnertoolbar/content/layoutdialog2.xul",
                 "dlg",
                 feature,
                 nhnJsStudy);
    

}

JsStudy.prototype.httpResponseMethod = function(aEvent){
    
    //var request = new XMLHttpRequest();
    //request.open("GET", "http://www.163.com", false);
    //request.send(null);
    //if(request.status == 200 ){
    //    //alert(request.responseText);
    //    
    //    var header = request.getResponseHeader("Date");
    //    alert(request.responseType);
    //    var headers = request.getAllResponseHeaders();
    //    //alert(headers);
    //}
    //else{
    //    alert("error with code:" + request.status);
    //}
    
    var feature = "centerscreen, chrome, dialog=no,resizable=yes";
    var dlg = window.openDialog("chrome://nhnjapanpartnertoolbar/content/httpdialog.xul",
                 "dlg",
                 feature,
                 nhnJsStudy);
    
}



JsStudy.prototype.openScrollDlg = function(aEvent){

    var feature = "centerscreen, chrome, dialog=no,resizable=yes";
    var dlg = window.openDialog("chrome://nhnjapanpartnertoolbar/content/scrolldialog.xul",
                 "dlg",
                 feature,
                 nhnJsStudy);
}

JsStudy.prototype.XMLDocLoader = function(aEvent){
    var xmlDoc = XML.newDocument();
    xmlDoc.async = false;
    xmlDoc.load("chrome://nhnjapanpartnertoolbar/content/scrolldialog.xul");
}

JsStudy.prototype.XBLTest = function(aEvent){
    window.openDialog(  
      "chrome://nhnjapanpartnertoolbar/content/bindingDialog.xul",  
      "xulschoolhello-xbl-window",  
      "chrome,resizable=yes,location,centerscreen");
    
}

JsStudy.prototype.testInOtherDlg = function(aEvent){
    window.alert("fdsa");
    
}


JsStudy.prototype.changeContextMenu = function(aEvent){
    //alert("fdsa");
    var item1 = document.getElementById("custommenuitem1");
    var item2 = document.getElementById("custommenuitem2");
    var x = item1.getAttribute("value");
    if(x == "1"){
        item1.setAttribute("hidden", "true");
        item2.setAttribute("hidden", "false");
        x = "0";
        
    }
    else{
        item2.setAttribute("hidden", "true");
        item1.setAttribute("hidden", "false");
        x = "1";
    }
    item1.setAttribute("value", x);
    
}

JsStudy.prototype.showCssBasicDialog = function(aEvent){
    var feature = "centerscreen, chrome, dialog=no,resizable=yes, modal";
    var dlg = window.openDialog("chrome://nhnjapanpartnertoolbar/content/CssBasic1Dialog.xul",
                 "dlg",
                 feature);
    
//    const ENIG_C = Components;
//    var g_service = null;
//    var g_myad = null;
//
//    try {
//		const ENIG_CONTRACTID = "@nhn.com/MyComponent;1";
//		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
//		g_service = Components.classes[ENIG_CONTRACTID].createInstance(Components.interfaces.IMyComponent);
//
//		g_myad = g_service.QueryInterface(Components.interfaces.IMyComponent);
//        var res = g_myad.Add(3, 4);
//        //window.alert(message);
//	} catch (err) {
//		alert(err);
//		return;
//	}
}

const NS_SHOW_PANEL = "showpanel";
const NS_SHOW_DIALOG = "showdialog";
const NS_SHOW_TOOLTIPS = "showtooltips";


JsStudy.prototype.showToopTips = function(aEvent){
    this.xpcomservice.OnToolbarActionByName(NS_SHOW_TOOLTIPS,null);
}


JsStudy.prototype.showModalDialog = function(aEvent){
    
    //JpToolbarCallback.targetWindow=window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
    // .getInterface(Components.interfaces.nsIWebNavigation)
    // .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
    // .treeOwner
    // .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
    // .getInterface(Components.interfaces.nsIXULWindow)
    // .docShell
    // .QueryInterface(Components.interfaces.nsIBaseWindow);
    // 
    // JpToolbarCallback.userAgent=window.navigator.userAgent;
    
    this.xpcomservice.OnToolbarActionByName(NS_SHOW_DIALOG,null);
}
